const CONFIG = require("../../config")
const knex = require('knex')(CONFIG.mysql);
module.exports = {
    async getList(req, res) {
        let items = {};
        let data;
        knex('blade_visual_category').select().then(resp => {
            //console.log(res)
            data = resp;
        }).then(() => {
            if (data.length > 0) {
                items = {
                    code: 200,
                    data: data,
                    msg: "操作成功",
                    success: true
                }
                res.send(items)
            } else {
                res.send("error");
            }
        })
    },
    async getDetail(req, res) {
        res.send('getDetail');
    },
    async postSave(req, res) {
        //1.拿到前端传递数据
        let { categoryKey, categoryValue } = req.body;

        knex('blade_visual_category').insert({ categoryKey: categoryKey, categoryValue: categoryValue, is_deleted: 1 }).then(resp => {
            console.log("res", resp);
            if (resp) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },
    async postUpdate(req, res) {
        let { categoryKey, categoryValue, id } = req.body;
        //console.log("---------------", req.body)

        knex('blade_visual_category').update({ categoryKey: categoryKey, categoryValue: categoryValue }).where({ id: id }).then(resp => {
            console.log(resp);
            if (resp === 1) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },

    async postRemove(req, res) {
        console.log("res._parsedUrl", req.query.ids);
        const ids = req.query.ids;

        knex('blade_visual_category').delete().where({ id: ids }).then(resp => {
            console.log(resp);
            if (resp) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },
}